<template>
  <BasicTable @register="registerTable">
    <template #footer>
      <div class="text-center pt-2 pb-2">
        <a-button type="primary" @click="handleSubmit">确定</a-button>
      </div>
    </template>
  </BasicTable>
</template>
<script lang="ts">
  import { defineComponent, ref, onMounted } from 'vue';
  import { BasicTable, useTable } from '/@/components/Table';
  import { columns, dataSource } from './mailboxConfiguration.data';
  import { getConfigUpdateEmail, getConfigEditEmailPage } from '/@/api/flowManage/flowManage';
  import { useMessage } from '/@/hooks/web/useMessage';

  export default defineComponent({
    name: 'MailboxConfiguration',
    components: { BasicTable },
    setup() {
      const { createMessage } = useMessage();
      const [registerTable, { setProps, setTableData, getDataSource }] = useTable({
        title: '',
        rowKey: 'id',
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: [],
          autoSubmitOnEnter: true,
        },
        scroll: { y: window?.innerHeight - 260 },
        useSearchForm: false,
        showTableSetting: false,
        bordered: true,
        showIndexColumn: false,
        canResize: false,
        pagination: false,
        immediate: false,
        // handleSearchInfoFn(info) {
        //   console.log('handleSearchInfoFn', info);
        //   return info;
        // },
        // showHeader: false,
      });
      const fetch = async () => {
        handleSetProps(true);
        const data = await getConfigEditEmailPage();
        let newDataSource = [...dataSource];
        if (data) {
          newDataSource.forEach((item) => {
            item.value = data[item.field];
          });
          setTableData(newDataSource);
        }
        handleSetProps(false);
      };
      onMounted(fetch);

      function handleEdit(record) {
        console.log(record);
      }

      function handleDelete(record) {
        console.log(record);
      }

      const handleSubmit = async () => {
        try {
          const data = getDataSource();
          let params: Recordable = {};
          data.forEach((item) => {
            params[item.field] = item.value;
          });
          console.log('params', params);
          handleSetProps(true);
          setProps;
          await getConfigUpdateEmail(params);
          createMessage.success('操作成功');
          fetch();
        } finally {
          handleSetProps(false);
        }
      };

      const handleSetProps = (bool) => {
        setProps({ loading: bool });
      };

      return {
        registerTable,
        handleEdit,
        handleDelete,
        handleSubmit,
      };
    },
  });
</script>
